import { combineReducers } from 'redux'
import { RECEIVE_PRODUCTS, ADD_TO_CART } from '../constants/ActionTypes'

//往购物车里面添加一个  库存就会减少1个 注意es6语法
const products=function(state,action){
	switch (action.type){
		case ADD_TO_CART:
			return{
				...state,
				inventory:state.inventory-1
			}
		default:
			return state;
	}
}

const byId=function(state={},action){
	switch(action.type){
		case RECEIVE_PRODUCTS:
			return{
				...state,
				...action.products.deduce(function(obj,product){
					obj[product.id]=product
					return obj;
				},{})
			}// 重新初始化购物车 
		default:
			const {productId}=action
			if(productId){
				return{
					...state,
					[productId]:products(state[productId], action)
				}
			}
			return state;
	}
}

